 # -*- coding: utf-8 -*-
from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *

import math

class Honeycomb():

  def __init__(self, D, X_N, Y_N):
    
    # 胞元几何形状参数
    self.D = D          # 外接圆直径
    self.R = 0.5 * D    # 外接圆半径
    
    # 阵列数目
    self.X_N = X_N
    self.Y_N = Y_N

    # 阵列间距
    self.X_SPACE = 3 * self.R
    self.Y_SPACE = 3 ** 0.5 * self.R

  # 生成胞元
  def generateCellUnit(self):

    def sin(theta):
      return math.sin(theta / 180.0 * math.pi)

    def cos(theta):
      return math.cos(theta / 180.0 * math.pi)
    
    R = self.R

    # 六边形的6个顶点(最右侧为编号1，逆时针编号)
    point_1 = (R * cos(0), R * sin(0), 0)
    point_2 = (R * cos(60), R * sin(60), 0)
    point_3 = (R * cos(120), R * sin(120), 0)
    point_4 = (R * cos(180), R * sin(180), 0)
    point_5 = (R * cos(240), R * sin(240), 0)
    point_6 = (R * cos(300), R * sin(300), 0)

    # 组成六边形的6条线段
    line_1 = (point_1, point_2)
    line_2 = (point_2, point_3)
    line_3 = (point_3, point_4)
    # 4，5，6号线段需注意端点的前后顺序
    # 保持与前三条线段方向一致，便于后面去除重复线段
    line_4 = (point_5, point_4)
    line_5 = (point_6, point_5)
    line_6 = (point_1, point_6)

    lines = [line_1, line_2, line_3, line_4, line_5, line_6]

    return lines

  # 沿x轴、y轴阵列线段
  def arrayCellUnit(self, x_n, y_n, cellUnitLines):

    X_SPACE = self.X_SPACE
    Y_SPACE = self.Y_SPACE

    X_arrayLines = []
    Y_arrayLines = []

    # 胞元沿x轴进行阵列
    for i in range(x_n):

      for item in cellUnitLines:

        point_x_1 = (item[0][0] + X_SPACE * i, item[0][1], item[0][2])
        point_x_2 = (item[1][0] + X_SPACE * i, item[1][1], item[1][2])
        
        X_arrayLines.append((point_x_1, point_x_2))
    
    # 胞元沿y轴进行阵列
    for j in range(y_n):

      for each in X_arrayLines:

        point_y_1 = (each[0][0], each[0][1] + Y_SPACE * j, each[0][2])
        point_y_2 = (each[1][0], each[1][1] + Y_SPACE * j, each[1][2])

        Y_arrayLines.append((point_y_1, point_y_2))

    return Y_arrayLines

  # 移动线段
  def move(self, offset_x, offset_y, lines):

    res = []

    for line in lines:
      point_1 = (line[0][0] + offset_x, line[0][1] + offset_y, 0)
      point_2 = (line[1][0] + offset_x, line[1][1] + offset_y, 0)
      res.append((point_1, point_2))

    return res

  def printParameters(self):

    print(" ")

    # 打印参数
    print("D: %s " % self.D)
    print("X_N: %d " % self.X_N)
    print("Y_N: %d " % self.Y_N)

  # 生成整体结构
  def part(self):

    R = self.R

    X_N = self.X_N
    Y_N = self.Y_N

    cellUnitLines = self.generateCellUnit()
    arrayLines_1 = self.arrayCellUnit(X_N, Y_N, cellUnitLines)

    offset_x = 1.5 * R
    offset_y = 0.5 * 3 ** 0.5 * R
    
    # 偏移六边形，进行第二次阵列
    movedCellUnitLines = self.move(offset_x, offset_y, cellUnitLines)
    arrayLines_2 = self.arrayCellUnit(X_N - 1, Y_N - 1, movedCellUnitLines)

    lines = arrayLines_1 + arrayLines_2

    # 去除重合的线段
    lines = list(set(lines))

    lines_tuple = tuple(lines)

    mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Part-1', type=DEFORMABLE_BODY)

    mdb.models['Model-1'].parts['Part-1'].WirePolyLine(mergeType=IMPRINT, meshable=ON, 
        points=lines_tuple)

    mdb.models['Model-1'].parts['Part-1'].Set(edges=
        mdb.models['Model-1'].parts['Part-1'].edges.getSequenceFromMask(('[#3 ]', 
        ), ), name='Wire-2-Set-1')

    self.printParameters()

def main(D, X_N, Y_N):
  star_1 = Honeycomb(D, X_N, Y_N)
  star_1.part()

if __name__ == '__main__':

  main(D=10.0, X_N=3, Y_N=5)

# Save by cgp on 2021_06_01-17.37.10; build 6.14-5 2015_08_18-22.37.49 135153